﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tigera.UICommon;
using Tigera.T_Definition;

namespace Plugin.TradeSharp
{
    public partial class UCRiskSettingRule2 : UserControl
    {
        public UCRiskSettingRule2()
        {
            InitializeComponent();
            this.listView_AllowStocks.FullRowSelect = true;
            this.listView_AllowStocks.MultiSelect = false;
            this.listView_AllowStocks.HeaderStyle = ColumnHeaderStyle.Clickable;


            this.listView_AllowStocks.View = System.Windows.Forms.View.Details;


        }
        public string Title
        {
            get
            {
                return "产品白名单设置";
            }
        }

        public List<RiskStockLimit> AllowStockSettings
        {
            get
            {
                List<RiskStockLimit> ret = new List<RiskStockLimit>();
                foreach (ListViewItem item in this.listView_AllowStocks.Items)
                {
                    RiskStockLimit data = new RiskStockLimit();

                    data.Code = item.Text;
                    data.FloatNum = string.IsNullOrEmpty(item.SubItems[1].Text) ? 0 : double.Parse(item.SubItems[1].Text);
                    data.FloatLimit = string.IsNullOrEmpty(item.SubItems[2].Text) ? 0 : double.Parse(item.SubItems[2].Text);
                    data.TotalNum = string.IsNullOrEmpty(item.SubItems[3].Text) ? 0 : double.Parse(item.SubItems[3].Text);
                    data.TotalLimit = string.IsNullOrEmpty(item.SubItems[4].Text) ? 0 : double.Parse(item.SubItems[4].Text);
                    data.PerInTolLimit = string.IsNullOrEmpty(item.SubItems[5].Text) ? 0 : double.Parse(item.SubItems[5].Text);
                    ret.Add(data);
                }
                return ret;

            }
            set
            {
                this.listView_AllowStocks.Items.Clear();

                foreach (var data in value)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = data.Code;
                    item.SubItems.Add(data.FloatNum.ToString());
                    item.SubItems.Add(data.FloatLimit.ToString());
                    item.SubItems.Add(data.TotalNum.ToString());
                    item.SubItems.Add(data.TotalLimit.ToString());
                    item.SubItems.Add(data.PerInTolLimit.ToString());
                    this.listView_AllowStocks.Items.Add(item);
                }

            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            var txt = textBox_ProductCode.Text;
            txt = txt.Trim();

            if (string.IsNullOrEmpty(txt))
            {
                textBox_ProductCode.Focus();
                return;
            }

            if (AllowStockSettings != null && AllowStockSettings.Select(o => o.Code).Contains(txt))
            {
                return;
            }
            var item = this.listView_AllowStocks.Items.Add(txt);
            item.SubItems.Add("1000000000");
            item.SubItems.Add("0.1");
            item.SubItems.Add("1000000000");
            item.SubItems.Add("0.05");
            item.SubItems.Add("0.1");
        }

        private void listView_AllowProducts_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.listView_AllowStocks.SelectedItems.Count > 0)
            {

                int selectindex = this.listView_AllowStocks.SelectedIndices[0];
                var txt = this.AllowStockSettings[selectindex];

                FormContainer form = new FormContainer();
                UCRiskSettingRule3 ctl = new UCRiskSettingRule3();
                ctl.setting = txt;

                form.SetControl(ctl);
                if (form.ShowDialog(this) == DialogResult.OK)
                {


                    this.listView_AllowStocks.Items[selectindex].SubItems[1].Text = ctl.setting.FloatNum.ToString();
                    this.listView_AllowStocks.Items[selectindex].SubItems[2].Text = ctl.setting.FloatLimit.ToString();
                    this.listView_AllowStocks.Items[selectindex].SubItems[3].Text = ctl.setting.TotalNum.ToString();
                    this.listView_AllowStocks.Items[selectindex].SubItems[4].Text = ctl.setting.TotalLimit.ToString();
                    this.listView_AllowStocks.Items[selectindex].SubItems[5].Text = ctl.setting.PerInTolLimit.ToString();
                }
            }


        }

        private void listView_AllowProducts_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && this.listView_AllowStocks.SelectedItems.Count > 0)
            {
                //ContextMenuStrip menuStrip = new ContextMenuStrip();

                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Name = "MenuClick_DeleteItem";
                item.Tag = "MenuClick_DeleteItem";
                item.Text = "删除";
                item.Click += new EventHandler(item_Click);
                contextMenuStrip1.Items.Add(item);
                //System.Drawing.Point _menuClickPt = this.PointToClient(e.Location);//.MousePosition);
                contextMenuStrip1.Show(e.Location);
                
            }
        }

        void item_Click(object sender, EventArgs e)
        {
            if (this.listView_AllowStocks.SelectedItems.Count > 0)
            {
                this.listView_AllowStocks.Items.RemoveAt(this.listView_AllowStocks.SelectedIndices[0]);
            }
        }
    }
}
